package com.bjfu.gmms.servlet;

import com.bjfu.gmms.MyBatisUtil;
import com.bjfu.gmms.mapper.AdminSecretaryMapper;
import com.bjfu.gmms.mapper.PhaseMapper;
import com.bjfu.gmms.model.Pairing;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/dispalyAreaServlet")
public class DispalyAreaServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try(SqlSession sqlSession = MyBatisUtil.getSqlSession("development")){
            PhaseMapper phaseMapper = sqlSession.getMapper(PhaseMapper.class);
            // 研究生管理秘书是5号 目前就一个信息学院 来不及做其他的了
            int period = phaseMapper.getPeriodByCollegeIdAndId(4, 5);
            if(period == 0){
                // 研究生管理秘书还没有公布
                // 当 period 为 0 时，显示一个弹窗
                response.setContentType("text/html;charset=UTF-8");
                response.getWriter().write("<html><head><title>弹窗</title></head><body>");
                response.getWriter().write("<script type='text/javascript'>");
                response.getWriter().write("alert('暂无公示结果');");
                response.getWriter().write("window.location.href = 'index.jsp';");
                response.getWriter().write("</script>");
                response.getWriter().write("</body></html>");
            } else {
                AdminSecretaryMapper mapper = sqlSession.getMapper(AdminSecretaryMapper.class);
                // 调用 Mapper 中的方法查询数据
                List<Pairing> pairingList = mapper.getCollegeMentorStudentPairing("004");
                request.getSession().setAttribute("pairingList", pairingList);
                request.getRequestDispatcher("allResultsQuery.jsp").forward(request, response);
            }
        }
    }
}
